global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_8_adao_simulation.log, replace name(tabl)

capture noi {

	clear
	qui do ${code_dir}/config/tabletools.do

	* Load the adao simulation p-values
	use ${final_dir}/p_values_country_4000.dta, clear
	forvalues col=1/9 {
		local pc_`col'_L = p_value_L[`col']
		local pc_`col'_H = p_value_H[`col']
	}

	use ${final_dir}/p_values_firmweightdomestic_4000.dta, clear
	forvalues col=1/9 {
		local pfd_`col'_L = p_value_L[`col']
		local pfd_`col'_H = p_value_H[`col']
	}

	use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear
	mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95.dta, unmatched(none)
	tab year, gen(YY_)

	estimates clear
	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar GDPPC = gdppcMPm_1995_a
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	sort lse_id year


	cap program drop vars
	program vars
		hasvar stock* spill*, local("stockspill") yes("\yes") no("\no")
		hasvar GDPGAP*, local("gdpgap") yes("\yes") no("\no")
		hasvar VAEMP*, local("vaemp") yes("\yes") no("\no")
		hasvar GDPPC*, local("gdppc") yes("\yes") no("\no")
	end program

	cap program drop pstat1
	program pstat1
		args pstat_L pstat_H
		assert "`pstat_L'" != ""
		assert "`pstat_H'" != ""
		mat pm1 = e(b)
		mat pm1[1,1] = `pstat_L'
		mat pm1[1,2] = `pstat_H'
		estadd matrix pm1
	end program

	cap program drop pstat2
	program pstat2
		args pstat_L pstat_H
		assert "`pstat_L'" != ""
		assert "`pstat_H'" != ""
		mat pm2 = e(b)
		mat pm2[1,1] = `pstat_L'
		mat pm2[1,2] = `pstat_H'
		estadd matrix pm2
	end program

	* 1) GDPGAP | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
	pstat1 `pc_1_L' `pc_1_H'
	pstat2 `pfd_1_L' `pfd_1_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_1

	matrix list e(b)

	* 2) GDPGAP + VAEMP | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
	pstat1 `pc_2_L' `pc_2_H'
	pstat2 `pfd_2_L' `pfd_2_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_2
	* 3) GDPGAP + GDPPC | F+IY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
	pstat1 `pc_3_L' `pc_3_H'
	pstat2 `pfd_3_L' `pfd_3_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\no"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_3

	* 4) GDPGAP | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	pstat1 `pc_4_L' `pc_4_H'
	pstat2 `pfd_4_L' `pfd_4_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_4
	* 5) GDPGAP + VAEMP | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	pstat1 `pc_5_L' `pc_5_H'
	pstat2 `pfd_5_L' `pfd_5_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_5
	* 6) GDPGAP + GDPPC | F+IY+CY
	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	pstat1 `pc_6_L' `pc_6_H'
	pstat2 `pfd_6_L' `pfd_6_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_6

	clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a	
	clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
	clonevar GDPGAP_foreign = lngdpgap_shr_foreign_1995_a
	clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
	clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

	* 7) GDPGAP | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	pstat1 `pc_7_L' `pc_7_H'
	pstat2 `pfd_7_L' `pfd_7_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_7
	* 8) GDPGAP + VAEMP | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	pstat1 `pc_8_L' `pc_8_H'
	pstat2 `pfd_8_L' `pfd_8_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_8
	* 9) GDPGAP + GDPPC | F+IY+CY | foreign
	ppmlhdfe F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPGAP_foreign GDPPC_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) vce(cluster lse_id)
	pstat1 `pc_9_L' `pc_9_H'
	pstat2 `pfd_9_L' `pfd_9_H'
	vars
	estadd local f "\yes"
	estadd local iy "\yes"
	estadd local cy "\yes"
	estadd local obs "{\num{`e(N)'}}"
	estadd local firms "{\num{`e(N_clust)'}}"
	estimates store col1995_9

	setlabels
	qui do ${code_dir}/config/tabletools.do

	dhoztab * using ${tab_dir}/main/Table_8_adao_simulation.tex, collabels(none) ///
	cells(b(star pvalue(pm1) fmt(%10.2f)) pm1(fmt(%10.3f) par([ ])) pm2(fmt(%10.3f) par(\{ \}))) ///
	nogaps replace numbers nolines nomtitles sfmt(a1) scalars("has_gdpgap \noalign{\medskip} GDP gap" "has_vaemp Labor productivity" "has_gdppc GDP per capita" "has_stockspill \noalign{\medskip} Stocks and spillovers" "f Firm fixed effects" "iy Industry \stimes year fixed effects" "cy Country \stimes year fixed effects" "obs \noalign{\medskip} Observations"  "firms Firms") label mgroups("Domestic and foreign" "Foreign", pattern(1 0 0 0 0 0 1 0 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) drop(stock* spill* GDPGAP VAEMP GDPPC _cons) noobs rename(LSW_foreign LSW HSW_foreign HSW GDPGAP_foreign GDPGAP VAEMP_foreign VAEMP GDPPC_foreign GDPPC) ///
		depvar("Auto95") ///
		notes("This table reproduces the baseline regression and performs two falsification tests. The first test addresses Adao et al. (2019)'s concern that in shift-share design, observations with similar weights distribution may have correlated errors, leading to an overrejection of the null hypothesis. For each country, we sample with replacement the entire path of macroeconomics variables (wages, labor productivity, GDP per capita, and GDP gap) from the existing set of countries. We then run the same regressions as in our baseline. We repeat 4000 times and compare the coefficients from the true regression with the distribution of coefficients from simulated regressions. The [ ] brackets report the p-values of our original coefficients. In a second exercise, we perform a similar exercise but instead of re-drawing the macro-variables, we re-draw firms' weights from the distribution of weights for firms in the same country. We run regressions on the simulated data and the \{ \} brackets state the p-values. Significance levels at *10\%, **5\% and ***1\% using the [ ] p-values.")


	*again bracktes from dhoztab
}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}


cap log close tabl

